package com.example.springbott.wx_controller;

import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.kerbal.WxCartCheckoutVo;
import com.example.springbott.bean.kerbal.WxCartIndexVo;
import com.example.springbott.wx_service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;



    /**
     * wx购物车goodscount
     *
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("goodscount")
    public BaseRespVo wxCartGoodscount() {
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            principal = new MarketUser();
        }
        Integer count = wxCartService.wxCartGoodscount(principal.getId());
        return BaseRespVo.ok(count);
    }



    /**
     * wx车快速添加
     *
     * @param map 地图
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("fastadd")
    public BaseRespVo wxCartFastAdd(@RequestBody Map map) {
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }
        Integer code = wxCartService.wxCartFastAdd(principal.getId(), goodsId, number, productId);
        if (code == 0) {
            return BaseRespVo.invalidData("网络繁忙，请重试");

        } else {
            return BaseRespVo.ok(code);
        }
    }



    /**
     * wx购物车添加
     * http://localhost:8083/wx/cart/add
     *
     * @param map 地图
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("add")
    public BaseRespVo wxCartAdd(@RequestBody Map map) {
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        //Integer goodsId, Integer number, Integer productId
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }
        wxCartService.wxCartFastAdd(principal.getId(), goodsId, number, productId);

            return BaseRespVo.ok(wxCartService.wxCartGoodscount(principal.getId()));

    }



    /**
     * wx车指数
     * http://localhost:8083/wx/cart/index
     *
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("index")
    public BaseRespVo wxCartIndex() {
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }
        WxCartIndexVo wxCartIndexVo = wxCartService.wxCartIndex(principal.getId());
        return BaseRespVo.ok(wxCartIndexVo);


    }



    /**
     * wx车检查
     *
     * @param map 地图
     * @return {@link BaseRespVo}
     */
    @RequestMapping("checked")
    public BaseRespVo wxCartChecked(@RequestBody Map map){
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            principal = new MarketUser();
        }
        int[] ints = new int[0];
        ArrayList<Integer> productIds = (ArrayList<Integer>) map.get("productIds");
        Integer isChecked = (Integer) map.get("isChecked");
        Integer code = wxCartService.wxCartChecked(productIds, isChecked, principal.getId());
        if (code==500){
            return BaseRespVo.invalidData("网络繁忙");
        }
        return BaseRespVo.ok(wxCartService.wxCartIndex(principal.getId()));
    }



    /**
     * wx购物车更新
     * http://182.92.235.201:8083/wx/cart/update
     * @param map 地图
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("update")
    public BaseRespVo wxCartUpdate(@RequestBody Map map){
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        //Integer goodsId, Integer number, Integer productId
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }
        Integer code = wxCartService.wxCartUpdate(goodsId, number, productId, principal.getId());
        if (code==1){
           return BaseRespVo.ok(null);
        }else {
         return    BaseRespVo.invalidData("网络繁忙");
        }
    }



    /**
     * wx购物车删除
     *
     * @param map 地图
     * @return {@link BaseRespVo}
     * @kerbal
     */
    @RequestMapping("delete")
    public  BaseRespVo wxCartDelete(@RequestBody Map map ){
        ArrayList<Integer> list = (ArrayList<Integer>) map.get("productIds");
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }
        Integer code = wxCartService.wxCartDelete(list, principal.getId());
        if (code==list.size()){
            return BaseRespVo.ok(wxCartService.wxCartIndex(principal.getId()));
        }else {
            return    BaseRespVo.ok("网络繁忙");
        }


    }


    //http://182.92.235.201:8083/wx/cart/checkout?cartId=0&addressId=0&couponId=1&userCouponId=100&grouponRulesId=0
    @RequestMapping("checkout")
    public BaseRespVo wxCartCheckout(Integer cartId,Integer addressId ,Integer couponId,Integer userCouponId,Integer grouponRulesId){
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return BaseRespVo.fail("请登录", 501);
        }

        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.wxCartCheckout(cartId, addressId, couponId, userCouponId, grouponRulesId, principal.getId());
      return   BaseRespVo.ok(wxCartCheckoutVo);

    }


}
